package org.frog.sgs.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import org.apache.commons.io.IOUtils;
import org.apache.mina.core.buffer.IoBuffer;

/**
 * int(event code)/int(length)/byte[]
 * 
 * @author duanxiangqi
 * @date 2012-10-22
 */
public class SgsMsg implements Serializable {

	private static final long serialVersionUID = -8032473978858607306L;

	private int code = -1;

	private int length = -1;

	private int position = 0;

	private ByteArrayOutputStream output = new ByteArrayOutputStream();

	private DataOutputStream os = new DataOutputStream(output);

	private DataInputStream is;

	public SgsMsg() {}

	public SgsMsg(int code) {
		this.code = code;
	}

	public void readToBuffer() {
		byte[] bytes = output.toByteArray();
		length = bytes.length;
		is = new DataInputStream(new ByteArrayInputStream(bytes));
	}

	public IoBuffer writeToBuffer() {
		IoBuffer buffer = IoBuffer.allocate(4 + 4 + output.size());
		buffer.putInt(code);
		buffer.putInt(output.size());
		byte[] bytes = output.toByteArray();
		buffer.put(bytes);
		buffer.flip();
		return buffer;
	}

	public void writeToStream(OutputStream os) throws IOException {
		readToBuffer();
		DataOutputStream dos = new DataOutputStream(os);
		dos.writeInt(code);
		dos.writeInt(length);
		IOUtils.copy(is, dos);
	}

	public void destory() {
		try {
			output.close();
		} catch (Exception e) {}
		try {
			os.close();
		} catch (Exception e) {}
		try {
			is.close();
		} catch (Exception e) {}
	}

	public byte readByte() throws IOException {
		return is.readByte();
	}

	public char readChar() throws IOException {
		return is.readChar();
	}

	public int readInt() throws IOException {
		return is.readInt();
	}

	public long readLong() throws IOException {
		return is.readLong();
	}

	public boolean readBoolean() throws IOException {
		return is.readBoolean();
	}

	public String readString() throws IOException {
		return is.readUTF();
	}

	public Object readObject() throws IOException, ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(is);
		Object obj = ois.readObject();
		ois.close();
		return obj;
	}

	public void writeByte(byte data) throws IOException {
		os.write(data);
	}

	public void writeChar(char data) throws IOException {
		os.writeChar(data);
	}

	public void writeInt(int data) throws IOException {
		os.writeInt(data);
	}

	public void writeLong(long data) throws IOException {
		os.writeLong(data);
	}

	public void writeBoolean(boolean data) throws IOException {
		os.writeBoolean(data);
	}

	public void writeString(String data) throws IOException {
		os.writeUTF(data);
	}

	public void writeObject(Object data) throws IOException {
		ObjectOutputStream oos = new ObjectOutputStream(os);
		oos.writeObject(data);
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public ByteArrayOutputStream getOutput() {
		return output;
	}

	public void setOutput(ByteArrayOutputStream output) {
		this.output = output;
	}

	public String toString() {
		return "SgsMsg [code=" + code + "]";
	}
}
